package com.kmxd.ams.app.archive;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kmxd.ams.app.event.AmsEventPublisher;
import com.kmxd.ams.client.archive.IArchiveAuthSvc;
import com.kmxd.ams.client.archive.dto.ArchiveAuthAddDTO;
import com.kmxd.ams.client.archive.dto.ArchiveAuthEditDTO;
import com.kmxd.ams.client.archive.dto.ArchiveAuthQueryDTO;
import com.kmxd.ams.client.archive.vo.ArchiveAuthListVO;
import com.kmxd.ams.client.archive.vo.ArchiveAuthVO;
import com.kmxd.ams.core.common.query.QueryGen;
import com.kmxd.ams.core.util.IdUtil;
import com.kmxd.ams.infra.archive.entity.ArchiveAuth;
import com.kmxd.ams.infra.archive.mapper.ArchiveAuthMapper;
import java.util.List;
import java.util.Set;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 通用权限设置 服务实现类
 *
 * @author nickbi
 * @since 2022-07-26
 */
@Service
@AllArgsConstructor
public class ArchiveAuthSvcImpl extends ServiceImpl<ArchiveAuthMapper, ArchiveAuth>
    implements IArchiveAuthSvc {

  private final AmsEventPublisher amsEventPublisher;
  private ArchiveAuthMapper archiveAuthMapper;

  @Override
  public Page<ArchiveAuthListVO> selectPage(Page reqPage, ArchiveAuthQueryDTO req) {
    LambdaQueryWrapper<ArchiveAuth> queryWrapper =
        QueryGen.init(new QueryWrapper<ArchiveAuth>(), req).lambda();
    Page<ArchiveAuth> page = archiveAuthMapper.selectPage(reqPage, queryWrapper);
    Page<ArchiveAuthListVO> voPage = BeanUtil.copyProperties(page, Page.class);
    voPage.setRecords(BeanUtil.copyToList(page.getRecords(), ArchiveAuthListVO.class));
    return voPage;
  }

  @Override
  public List<ArchiveAuthListVO> selectList(ArchiveAuthQueryDTO req) {
    LambdaQueryWrapper<ArchiveAuth> queryWrapper =
        QueryGen.init(new QueryWrapper<ArchiveAuth>(), req).lambda();
    List<ArchiveAuth> list = archiveAuthMapper.selectList(queryWrapper);
    return BeanUtil.copyToList(list, ArchiveAuthListVO.class);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void add(ArchiveAuthAddDTO req) {
    ArchiveAuth entity = BeanUtil.copyProperties(req, ArchiveAuth.class);
    archiveAuthMapper.insert(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void edit(ArchiveAuthEditDTO req) {
    ArchiveAuth entity = BeanUtil.copyProperties(req, ArchiveAuth.class);
    archiveAuthMapper.updateById(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void remove(String ids) {
    Set<Long> idList = IdUtil.split(ids);
    this.baseMapper.deleteBatchIds(idList);
  }

  @Override
  public ArchiveAuthVO view(Long id) {
    ArchiveAuth entity = archiveAuthMapper.selectById(id);
    return BeanUtil.copyProperties(entity, ArchiveAuthVO.class);
  }

  @Override
  public ArchiveAuthVO getByArchTypeId(Long id) {
    LambdaQueryWrapper<ArchiveAuth> wrapper =
        Wrappers.<ArchiveAuth>lambdaQuery().eq(ArchiveAuth::getArchTypeId, id);
    ArchiveAuth entity = archiveAuthMapper.selectOne(wrapper);
    return BeanUtil.copyProperties(entity, ArchiveAuthVO.class);
  }
}
